<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Payorder extends Controller
{
    private $_model;
    private $title_field = [
    ];
    public $pagesize = 10;

    public function _initialize()
    {
        $this->_model = new \app\admin\model\PayOrder();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }

    public function getRendPosionInfo($id)
    {
        $model = new \app\admin\model\RentDevice();
        return $model->getDataForWhere(['id' => $id]);
    }

    public function getRendPositonLists()
    {
        $model = new \app\admin\model\RentPosition();
        $lists = $model->getDataForWheres([]);
        $lists = changeKeyForList($lists, 'id');
        $this->assign('positionLists', $lists);
        return $lists;
    }

    public function lists()
    {
        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $ser_stats = trim(input("request.ser_stats"));//auth
        $positionLists = $this->getRendPositonLists();

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }

        if ($ser_stats == '0' || $ser_stats == '1' || $ser_stats == '2' || $ser_stats == '3' || $ser_stats == '4') {
            $where['status'] = $ser_stats;
        }
        $where['is_delete'] = '0';
        $where['ty'] = array('in', [1, 2]);
        $lists = $this->_model->lists($where, $this->pagesize);
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');
        $lists['data'] = (new \app\admin\mapper\Payorder())->listMapper($lists['data']);
        foreach ($lists['data'] as $key => $val) {
            if ($val['ty'] == 1) {
                $lists['data'][$key]['goodLists'] = getGoodListByOrdersn($val['order_sn']);
            } else if ($val['ty'] == 2) {
                $lists['data'][$key]['devInfo'] = $this->getRendPosionInfo($val['rent_id']);
            }
        }
//        print_r($lists);
        $this->assign('lists', $lists);
        $this->assign('request', input("request."));
        return view('lists');
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('is_delete' => 1), $id);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    public function close()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $info = $this->_model->getDataForWhere(array('id' => $id));
        $res = $this->_model->updateData(array('status' => 4), $id);
        if ($res) {
            addOrderTrackLog(array(
                'ctime' => date("Y-m-d H:i:s"),
                'order_sn' => $info['order_sn'],
                'op_uid' => $this->LoginUserInfo['UserInfo']['id'],
                'op_username' => $this->LoginUserInfo['UserInfo']['uname'] . "[" . $this->LoginUserInfo['UserInfo']['nickname'] . "]",
                'act' => "管理员后台取消订单",
            ));
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    public function addGoodAttr($id, $attr)
    {
        $model = new \app\admin\model\GoodAttribute();
        $data = array(
            'good_id' => $id,
            'attr' => $attr,
        );
        $model->add($data);
    }

    public function addData()
    {
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $content = trim(input("request.content"));//auth
            $attr = trim(input("request.attr"));//auth
            $data2 = array(
                'content' => $content,
                'uid' => $this->LoginUserInfo['UserInfo']['id'],
                'ctime' => time(),
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->add($data);
            if ($res) {
                $this->addGoodAttr($res, $attr);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }
        $title = "添加";
        $this->assign('title', $title);
        return view('addData');
    }

    public function runRewardDate()
    {
        $days = trim(input("request.days"));//auth
        $task_time = trim(input("request.task_time"));//auth
        $open_reward_time = $this->runRewardTime($days, $task_time);
        $return = ['err' => 0, 'msg' => '操作成功', 'data' => $open_reward_time];
        return json($return);
    }

    public function infoData()
    {
        $id = trim(input("request.id"));//auth
        $info = $this->_model->getDataForWhere(array('id' => $id));
        $info['UserInfo'] = array();
        if ($info['uid']) {
            $info['UserInfo'] = getUserInfoForID($info['uid']);
        }

        if ($info['ty'] == 1) {
            $info['goodLists'] = getGoodListByOrdersn($info['order_sn']);
        } else if ($info['ty'] == 2) {
            $info['devInfo'] = $this->getRendPosionInfo($info['rent_id']);
        }

        $info = (new \app\admin\mapper\Payorder())->infoMapper($info);
        $this->assign('info', $info);
        $title = "详情";

        //获取订单日志
        $orderLogModel = new \app\admin\model\OrderTrackLog();
        $orderLogLists = $orderLogModel->getDataForWheres(array('order_sn' => $info['order_sn']), "*", "id desc");
        $this->assign('orderLogLists', $orderLogLists);

        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('infoData');

    }

    /**
     * 发货
     * @return \think\response\Json|\think\response\View
     */
    public function updateWuLiu()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $data1 = $_REQUEST['data'];

            $w = array(
                'id' => $id
            );
            $data2 = array();
            $data = array_merge($data1, $data2);
            $res = $this->_model->updateDataForWhere($data, $w);
            if ($res || ($res === 0)) {
                addOrderTrackLog(array(
                    'ctime' => date("Y-m-d H:i:s"),
                    'order_sn' => $info['order_sn'],
                    'op_uid' => $this->LoginUserInfo['UserInfo']['id'],
                    'op_username' => $this->LoginUserInfo['UserInfo']['uname'] . "[" . $this->LoginUserInfo['UserInfo']['nickname'] . "]",
                    'act' => "修改物流信息",
                ));
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


        $this->assign('info', $info);
        $allkdCompayList = getKdCompay();
        $this->assign('allkdCompayList', $allkdCompayList);
        //设置全局信息
        $title = "修改物流信息";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('updateWuLiu');
    }

    /**
     * 发货
     * @return \think\response\Json|\think\response\View
     */
    public function fahuo()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $data1 = $_REQUEST['data'];

            $w = array(
                'id' => $id
            );
            $data2 = array(
                'fhtime' => date("Y-m-d H:i:s"),
                'status' => 2
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->updateDataForWhere($data, $w);
            if ($res || ($res === 0)) {
                addOrderTrackLog(array(
                    'ctime' => date("Y-m-d H:i:s"),
                    'order_sn' => $info['order_sn'],
                    'op_uid' => $this->LoginUserInfo['UserInfo']['id'],
                    'op_username' => $this->LoginUserInfo['UserInfo']['uname'] . "[" . $this->LoginUserInfo['UserInfo']['nickname'] . "]",
                    'act' => "发货",
                ));
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


        $this->assign('info', $info);
        $allkdCompayList = getKdCompay();
        $this->assign('allkdCompayList', $allkdCompayList);

        //设置全局信息
        $title = "发货";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('fahuo');
    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $content = trim(input("request.conts"));//auth
            $attr = trim(input("request.attr"));//auth
            $w = array(
                'id' => $id
            );
            $data2 = array(
                'content' => $content,
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->updateDataForWhere($data, $w);

            if ($res || ($res === 0)) {
                $this->editGoodAttr($id, $attr);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }
        $tmpGoodAttribute = getGoodAttr($info['id']);
        $goodAttribute = array();
        if ($tmpGoodAttribute) {
            $goodAttribute = json_decode($tmpGoodAttribute['attr'], true);
        }
        $this->assign('goodAttribute', $goodAttribute);

        $this->assign('info', $info);
        //设置全局信息
        $title = "编辑";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');
    }

    public function saleLists()
    {
        $class_id = trim(input("request.class_id"));//auth
        $ser_username = trim(input("request.ser_username"));//auth
        $ser_min_num = trim(input("request.ser_min_num"));//auth
        $ser_max_num = trim(input("request.ser_max_num"));//auth
        $ser_is_win = trim(input("request.ser_is_win"));//auth
        $ser_is_yuer = trim(input("request.ser_is_yuer"));//auth
        if ($ser_min_num && $ser_max_num) {
            $where['num'] = array(
                array('egt', $ser_min_num), array('elt', $ser_max_num)
            );
        }
        if (!empty($ser_username)) {
            $tempUserLists = getUserListsForNickname($ser_username, 'id');
            $userIds = array();
            foreach ($tempUserLists as $key => $val) {
                $userIds[] = $val['id'];
            }
            $where['uid'] = array(
                'in', $userIds
            );
        }
        if ($ser_is_win == '0' || $ser_is_win == '1' || $ser_is_win == "2") {
            $where['is_win'] = $ser_is_win;
        }

        if ($ser_is_yuer == '0' || $ser_is_yuer == '1') {
            $where['is_yuer'] = $ser_is_yuer;
        }

        $where['classid'] = $class_id;
        $classUserjoinModel = new \app\admin\model\ClassUserjoin();
        $lists = $classUserjoinModel->lists($where, $this->pagesize);
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');

        $this->assign('lists', $lists);
        $this->assign('class_id', $class_id);

        $this->assign('request', input("request."));
        return view('saleLists');
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function comfireShouhuo()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $info = $this->_model->getDataForWhere(array('id' => $id));
        if ($info['status'] != 2) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('status' => 3), $id);
        if ($res) {
            addOrderTrackLog(array(
                'ctime' => date("Y-m-d H:i:s"),
                'order_sn' => $info['order_sn'],
                'op_uid' => $this->LoginUserInfo['UserInfo']['id'],
                'op_username' => $this->LoginUserInfo['UserInfo']['uname'] . "[" . $this->LoginUserInfo['UserInfo']['nickname'] . "]",
                'act' => "管理员后台确认收货",
            ));
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }

}